package com.mezz.bones.im.logic.service.impl;

import com.mezz.bones.im.logic.domain.entity.ChatGroupInfo;
import com.mezz.bones.im.logic.domain.request.ApiGroupInfoCreateRequest;
import com.mezz.bones.im.logic.repository.IChatGroupInfoRepository;
import com.mezz.bones.im.logic.service.IChatGroupInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
@AllArgsConstructor
public class ChatGroupInfoServiceImpl implements IChatGroupInfoService {

    private final IChatGroupInfoRepository groupInfoRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createGroupInfo(ApiGroupInfoCreateRequest request) {

        Date now = new Date();

        ChatGroupInfo groupInfo = new ChatGroupInfo();
        groupInfo.setGroupNo(request.getGroupNo());
        groupInfo.setGroupName(request.getGroupName());
        groupInfo.setDescription(request.getDescription());
        groupInfo.setAvatarUrl(request.getAvatarUrl());
        groupInfo.setMemberCount(0);
        groupInfo.setMaxMemberCount(500);
        groupInfo.setStatus(1);
        groupInfo.setDissolveTime(null);
        groupInfo.setLastChatTime(null);
        groupInfo.setCreateBy(request.getOperatorId());
        groupInfo.setCreateTime(now);
        groupInfo.setCreateName(request.getOperatorName());
        groupInfo.setUpdateBy(now);
        groupInfo.setUpdateTime(now);
        groupInfo.setUpdateName(request.getOperatorName());
        groupInfo.setIsDel(0);
        groupInfoRepository.save(groupInfo);
    }
}
